import * as chalk from 'chalk';
import { transports, format } from 'winston';
import 'winston-daily-rotate-file';

// 日志打印样式
const consoleLog = new transports.Console({
  format: format.combine(
    format.colorize(),
    format.printf(({ context, level, message, time }) => {
      const appStr = chalk.green(`[Matcha]`);
      const contextStr = chalk.yellow(`[${context}]`);

      return `${appStr} ${time} ${level} ${contextStr} ${message} `;
    })
  )
});

// 日志按日管理
const dailyRotateLog = new transports.DailyRotateFile({
  dirname: 'logs',
  filename: 'matcha-%DATE%.log',
  datePattern: 'YYYY-MM-DD HH',
  maxSize: '5m',
  maxFiles: '30d'
});

// 导出配置
export const WinstonConfig = {
  level: 'debug',
  transports: [consoleLog, dailyRotateLog],
  rejectionHandlers: [dailyRotateLog]
};
